<?php
namespace XtwsBack\Controller;
use Think\Controller;
class UserController extends Controller{
    public function index(){
        $member=M('member');
		$where="";
		if($_GET['keyword']!=''){
			$where="nickname like '%".$_GET['keyword']."%'";
			$this->assign('keyword',$_GET['keyword']);// 赋值分页输出
		}
        $count = $member->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member_arr =$member->where($where)->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($member_arr as $k => $v){
            if ($v['logintime'] == 0){
                $member_arr[$k]['logintime'] = '';
            }
        }
        $this->assign('list',$member_arr);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun index
    public function update(){
        $member = M('member');
        $id = $_GET['id']?$_GET['id']:$_POST['id'];
        $user =  $member->where("id = {$id}")->find();
        if (IS_POST){
            foreach($_POST as $k=>$v){
                if ($v=='' || $v == $user[$k]){
                    unset($_POST[$k]);
                }//end if
            }//end foreach
            if (!empty($_POST)){
                if($_POST['pwd']){
                    $_POST['pwd'] = md5($_POST['pwd']);
                }
                $update_member = $member->where("id = {$id}")->save($_POST);
                if ($update_member){
                    $this->success('保存成功','index.html');
                    die;
                }else{
                    $this->error('保存失败');
                }//end if
            }else{
                $this->error('无修改','index.html');
            }//end if

        }//end if $_POST
        if ($id){
            $this->assign('user',$user);
            $this->display();
        }else{
            $this->error('参数错误');
        }//end if
    }//end fun update
}//end class